﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraGrid;
using BusinessObjects;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class NgachLuongListView : Form
    {
        private BindingList<NgachLuong> bindingListNgachLuong;
        private NgachLuong _ngachLuong;

        public NgachLuongListView()
        {
            InitializeComponent();
        }


        private void NgachLuongListView_Load(object sender, EventArgs e)
        {
            string[] listFielName = new string[] { "MaQuanLy", "TenNgachLuong" };
            string[] listHeaderName = new string[] { "Mã Quản Lý", "Tên Ngạch Lương" };
            AppGridView.InitGridView(gvNgachLuong, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gvNgachLuong);
            AppGridView.ShowField(gvNgachLuong, listFielName, listHeaderName);

            LoadData();

        }
        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _ngachLuong = new NgachLuong();
            NgachLuongDetailView detailView = new NgachLuongDetailView(_ngachLuong);
            OpenDetailForm(detailView);
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }
        private void gcNgachLuong_DoubleClick(object sender, EventArgs e)
        {
            int rowSelected = gvNgachLuong.FocusedRowHandle;
            _ngachLuong = gvNgachLuong.GetRow(rowSelected) as NgachLuong;

            NgachLuongDetailView detailView = new NgachLuongDetailView(_ngachLuong);
            OpenDetailForm(detailView);
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Xóa một dòng thông tin học kỳ", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _ngachLuong = gvNgachLuong.GetFocusedRow() as NgachLuong;
                if (_ngachLuong != null && _ngachLuong.Oid >0)
                    if (DataAccessObject.NgachLuong.Delete(_ngachLuong) > 0)
                    {
                        gvNgachLuong.DeleteSelectedRows();
                        gvNgachLuong.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void LoadData()
        {
            List<NgachLuong> list = DataAccessObject.NgachLuong.GetListNgachLuong();
            bindingListNgachLuong = new BindingList<NgachLuong>(list);

            bindingSourceNgachLuong.DataSource = bindingListNgachLuong;
            gvNgachLuong.Invalidate();
        }

        private void OpenDetailForm(NgachLuongDetailView detailView)
        {
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.DeleteChanged += new EventHandler(detailView_DeleteChanged);
            detailView.ShowDialog();
        }

        internal void detailView_DeleteChanged(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            bindingListNgachLuong.Add(_ngachLuong);
            gvNgachLuong.Invalidate();
        }


    }
}
